package com.wxyzz.cem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.interceptor.UserInterceptor;
import com.wxyzz.cem.pojo.ClassInfo;
import com.wxyzz.cem.pojo.PaymentProject;
import com.wxyzz.cem.pojo.PaymentRecord;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.service.PaymentProjectService;
import com.wxyzz.cem.service.PaymentRecordService;
import com.wxyzz.cem.service.UserService;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 缴费项目 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/payment-project")
public class PaymentProjectController {

    @Autowired
    private PaymentProjectService paymentProjectService;
    @Autowired
    private PaymentRecordService paymentRecordService;
    @Autowired
    private UserService userService;
    @GetMapping("/classId")
    @UserInterceptor.UserIntercept
    public Result<Page<PaymentProject>> getAllProjectByPage(@RequestParam(value = "page", defaultValue = "1" ,required = false) Integer page,
                                                            @RequestParam(value = "size", defaultValue = "10",required = false) Integer size,
                                                            @RequestParam(required = false) String text){
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        Integer userId = user.getId();
        Integer classId = user.getClassId();
        Page<PaymentProject> paymentProject = paymentProjectService.getAllProjectByPage(page,size,text,classId,userId);
        return  Result.success(paymentProject);
    }

    @GetMapping("/{paymentProjectId}")
    @UserInterceptor.UserIntercept
    public Result<PaymentProject> getOneProjectById(@PathVariable Integer paymentProjectId){
        return Result.success(paymentProjectService.getById(paymentProjectId));
    }

    @PostMapping
    @UserInterceptor.UserIntercept
    public Result<Object> addProject( @RequestBody PaymentProject paymentProject){
        Integer userId = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class).getId();
        User user = userService.getById(userId);
        Integer classId = user.getClassId();
        paymentProject.setClassId(classId);
        return Result.success(paymentProjectService.save(paymentProject));

    }

    @PutMapping
    @UserInterceptor.UserIntercept
    public Result updateProject(@RequestBody PaymentProject paymentProject){
        return Result.success(paymentProjectService.updateById(paymentProject));
    }

    @DeleteMapping("/{paymentProjectId}")
    @UserInterceptor.UserIntercept
    public Result deleteProject(@PathVariable Integer paymentProjectId){
        return Result.success(paymentProjectService.deleteProject(paymentProjectId));
    }

    @PostMapping("/pay")
    @UserInterceptor.UserIntercept
    public Result payProject(@RequestBody PaymentRecord paymentRecord){
        Integer userId = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class).getId();
        paymentRecord.setUserId(userId);
        PaymentProject payProject = paymentProjectService.getById(paymentRecord.getPaymentProjectId());
        ClassInfo classInfo = Db.getById(payProject.getClassId(), ClassInfo.class);
        paymentRecord.setBeforeClassBalance(classInfo.getBalance());
        paymentRecord.setClassId(classInfo.getId());
        classInfo.setBalance(classInfo.getBalance()+payProject.getAmount());
        Db.updateById(classInfo);
        return Result.success(paymentRecordService.save(paymentRecord));
    }

}
